<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddColumnWorkDirInProjectTable extends AbstractMigration
{
    public function up()
    {
        try {

            $this->table('project')->addColumn('work_dir', 'string', [
                'limit' => 128,
                'null' => false,
                'default' => '',
                'comment' => '工作目录名称'
            ])->save();
            $this->updateWorkDirData();
        } catch (Throwable $e) {

        }
    }

    public function down()
    {
        $this->table('project')->removeColumn('work_dir')->save();
    }

    private function updateWorkDirData()
    {
        $this->table('project')->getAdapter()->execute('update project set work_dir = code where type=\'insource\'');
        $this->table('project')->getAdapter()->execute('update project p1 join project p2 ON p1.belong_project_id = p2.id  set p1.work_dir = p2.code where p1.type=\'outsource\'');
    }

}
